import BaseVenueProcessor from './BaseVenueProcessor.js';
import { getDongGuanSportsCenterVenueSettings } from '../request.js';

/**
 * 东莞市体育中心场地数据处理器
 */
export class DongGuanTiYuZhongXinProcessor extends BaseVenueProcessor {
  constructor() {
    super();
    this.venueName = '东莞市体育中心';
  }

  /**
   * 获取东莞市体育中心场地预约设置
   * @param {Object} params - 查询参数 { gym_id, type, date }
   * @returns {Promise} 请求Promise
   */
  async getVenueSettings(params = {}) {
    // 设置默认参数
    const defaultParams = {
      gym_id: 5,
      type: 'site',
      date: '2025-8-19'
    };
    
    // 合并参数
    const requestData = { ...defaultParams, ...params };
    
    return await getDongGuanSportsCenterVenueSettings(requestData);
  }

  /**
   * 验证数据是否有效
   * @param {Object} data - 原始场地数据
   * @returns {boolean} 数据是否有效
   */
  validateData(data) {
    return data && Array.isArray(data);
  }

  /**
   * 按场地名称分组
   * @param {Object} data - 原始场地数据
   * @returns {Map} 场地Map
   */
  groupVenues(data) {
    const venueMap = new Map();
    
    data.forEach(item => {
      if (item.site) {
        const venueName = item.site.name;
        if (!venueMap.has(venueName)) {
          venueMap.set(venueName, {
            name: venueName,
            siteId: item.site.id,
            timeSlots: []
          });
        }
      }
    });
    
    return venueMap;
  }

  /**
   * 处理时间槽
   * @param {Map} venueMap - 场地Map
   * @param {Object} data - 原始场地数据
   */
  processTimeSlots(venueMap, data) {
    venueMap.forEach((venue, venueName) => {
      // 找到对应的场地数据
      const venueData = data.find(item => item.site && item.site.name === venueName);
      
      if (venueData && venueData.sch && Array.isArray(venueData.sch)) {
        // 遍历时间间隔信息
        venueData.sch.forEach(timeInterval => {
          const timeSlot = `${timeInterval.start_time}-${timeInterval.end_time}`;
          const beginDatetime = `${this.selectedDate} ${timeInterval.start_time}`;
          const endDatetime = `${this.selectedDate} ${timeInterval.end_time}`;
          
          // 添加时间槽信息
          venue.timeSlots.push({
            time: timeSlot,
            isFree: !timeInterval.is_reserved && !timeInterval.is_disabled,
            price: parseFloat(timeInterval.price) || 0,
            beginDatetime: beginDatetime,
            endDatetime: endDatetime
          });
        });
      }
    });
  }

  /**
   * 转换为场地列表
   * @param {Map} venueMap - 场地Map
   * @returns {Array} 场地列表
   */
  convertToVenueList(venueMap) {
    return Array.from(venueMap.values()).map((venue, index) => {
      return {
        id: 19000 + index, // 给东莞市体育中心场地分配ID
        name: venue.name,
        address: '东莞市体育中心',
        latitude: 23.025707,
        longitude: 113.752252,
        phone: '0769-66778899',
        rating: 4.5,
        price: 0,
        facilities: ['室外场地', '专业教练'],
        image: '',
        distance: 8.5,
        timeSlots: venue.timeSlots,
        dataSource: this.venueName
      };
    });
  }
}

export default DongGuanTiYuZhongXinProcessor;